<?php
/**
 * 用户处理model
 * Created by Forever.
 * User: Forever
 * Date: 2018/10/03 
 * Time: 上午 10:39
 */
namespace app\api\model;
use think\Cache;
use \think\Model;
use \think\db;
use EasyWeChat\Foundation\Application;
class Member extends Base{

    public function initialize() { } 

    /*
    * 获取会员详情
    * @param data
    * @return object
    */
    public function getMemberDetail($data=[]){
        $where = [
            "openid"=>$data['openid']
        ];
        $field = "id,name,nickname,cs_id,mobile,headimg,province,city,district,address,addtime";
        $res =  Db::name(self::$tb['member'])->where($where)->field($field)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }


   /*
    * 获取会员收藏的店铺列表
    * @param data
    * @return object
    */
    public function getShopFollow($data=[]){ 
        $where = [
            "f.member_id"=>$data['member_id'],
            "s.ischeck"=>1
        ]; 
        $field = "s.id,s.name,s.province,s.city,s.district,s.shop_pic,f.addtime";
        $list =  Db::name(self::$tb['follow_shop'])->alias("f")->join("jun_shop s",'s.id=f.shop_id')
        ->where($where)->field($field)->select(); 
        foreach ($list as $key => $value) {
            $prepAddr = $value['province'].$value['city'].$value['district'];
            $list[$key]['latng'] = $this->get_localtion($data['latitude'],$data['longitude'],$prepAddr);
        }
        if($list){
            return $list;
        }else{
            return false;
        }
    }

   /*
    * 获取定位
    */
    public function get_localtion($latitude,$longitude,$prepAddr){  
       $data = $this->data;
       $key = config("baiduKey"); 
       $latng = getLatng($prepAddr); 
       $url = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=".$latitude.",".$longitude."&destinations=".$latng['lat'].",".$latng['lng']."&ak=".$key;
       $geocode = file_get_contents($url);
       $output= json_decode($geocode,true); 
       return $output['result']; 
    }
    

  /*
    * 获取会员收藏的汽车列表
    * @param data
    * @return object
    */
    public function getCarFollow($data=[]){
        $where = [];
        $field = "id,product_id,title,pic,first_pay,monthly_pay,addtime";
        $res =  Db::name(self::$tb['follow_product'])->where($where)->field($field)->order("addtime desc")->select();  
        return $res; 
    }

    



 }   
